package xuan.mall.system.infrastructure.security.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import xuan.mall.system.infrastructure.security.filter.MallLoginFilter;
import xuan.mall.system.infrastructure.security.provider.UserAuthenticationProvider;

import java.util.Collections;

/**
 * 认证配置
 */
@Configuration
@RequiredArgsConstructor
class AuthenticationConfig {

    private final UserAuthenticationProvider userAuthenticationProvider;

    void configure(HttpSecurity http) throws Exception {
        // 添加自定义登录过滤器
        http
                .addFilterBefore(
                        new MallLoginFilter(authenticationManager()),
                        UsernamePasswordAuthenticationFilter.class
                );
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Collections.singletonList(userAuthenticationProvider));
    }
}